*******************************************************
* Author: Michael Tomz
* Date: Dec 20, 2001
* Format: Stata do-file
* Purpose: Replicate Section 5.2 (Expected Vote Shares)
*******************************************************

version 6.0
clear
set mem 40m

* LOAD THE RESULTS
use r92, clear
append using r87
append using r83
append using r79
append using r74a
append using r74
append using r70
append using r66
append using r64
append using r59

* SAVE RESULTS FOR GRAPHING (SPLUS GRAPHIC COMMANDS BELOW)
gen ev1of12 = ev1/(ev1+ev2)   /* kk expected vote for party 1 as a share of the 2-party vote */
label var ev1of12 "Conserv share of Con+Lib Vote"
preserve
keep fully ev1 ev2 ev3 ev1c ev2c ev3c ev1of12
save evplot, replace old
restore

* CORRELATIONS BETWEEN EXPECTED VOTES PRODUCED BY KK AND SUR
di "Conservatives: Correlation btwn Expected Vote (KK) and Expected Vote (SUR)
corr ev1 ev1c if fully==1
di "Labour: Correlation btwn Expected Vote (KK) and Expected Vote (SUR)
corr ev2 ev2c if fully==1
di "Liberals: Correlation btwn Expected Vote (KK) and Expected Vote (SUR)
corr ev3 ev3c if fully==1
di "Cons % of Con+Lab: Correlation btwn Expected Vote (KK) and Expected Vote (SUR)
corr ev1of12 ev1c if fully==0

* TABLE OF ABSOLUTE ERRORS FOR KK MINUS ABSOLUTE ERRORS VIA SUR
recode year 59=1 64=2 66=3 70=4 74=5 74.9=6 79=7 83=8 87=9 92=10
quietly {   
   gen kk1 = 100*abs(v1-ev1) if ev1~=. & ev1c~=. & fully == 1
   gen kk2 = 100*abs(v2-ev2) if ev2~=. & ev2c~=. & fully == 1
   gen kk3 = 100*abs(v3-ev3) if ev3~=. & ev3c~=. & fully == 1
   gen sur1 = 100*abs(v1-ev1c) if ev1~=. & ev1c~=. & fully == 1
   gen sur2 = 100*abs(v2-ev2c) if ev2~=. & ev2c~=. & fully == 1
   gen sur3 = 100*abs(v3-ev3c) if ev3~=. & ev3c~=. & fully == 1
   * for partially contested districts, calculate KK error wrt share of the two-party vote
   replace kk1 = 100*abs(v1-(ev1/(ev1+ev2))) if ev1~=. & ev1c~=. & fully == 0
   replace sur1 = 100*abs(v1-ev1c) if ev1~=. & ev1c~=. & fully == 0
}
tempname abserr
matrix `abserr' = J(3,3,0)
matrix rownames `abserr' = KK SUR DIFF
matrix colnames `abserr' = Con Lab Lib
local i 1
while `i' <= 3 {
   su kk`i' if fully==1, meanonly         /* KK */
   matrix `abserr'[1,`i'] = r(mean)
   su sur`i' if fully==1, meanonly        /* SUR-Clarify */
   matrix `abserr'[2,`i'] = r(mean)
   matrix `abserr'[3,`i'] = `abserr'[1,`i'] - `abserr'[2,`i']  /* difference */
   local i = `i' + 1
}

di "Average Absolute Errors: Fully Contested Districts"
mat list `abserr'
di "Average Absolute Errors: Partially Contested Districts"
su kk1 sur1 if fully==0
